<?php
namespace app\admin\controller; 
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
class Games extends Base {
	
	//资讯对应游戏的编辑
	public function games(){
		$i_id = I('infoid/d',0);

        if($i_id){
            $g_info = D('info_game')->where('infoid',$i_id)->find();
			
			$goods = [];
			if(!empty($g_info)){
				$goods = M('goods')->whereIn('id' , $g_info['goodsid'])->field("gname,price,id,icon")->select();
			}
 			
			$infos = M('Information')->where('id' , $i_id)->field("gname,id,icon,gameurl")->find();
			$g_info['gameurl'] = $infos['gameurl'];
	        $this->assign('info',$g_info);
			$this->assign('g_goods',$goods);
			$this->assign('iinfo',$infos);
        }
		
        return $this->fetch();
	}
	
	public function gameHandle(){
    	$data = I('post.');
		$gid = I('gameid/d',0);
		
        $validate = \think\Loader::validate('Games');
        if (!$validate->batch()->check($data)) {
            $error = $validate->getError();
            $error_msg = array_values($error);
            $return_arr = array(
                'status' => -1,
                'msg' => $error_msg[0],
                'data' => $error,
            );
            $this->ajaxReturn($return_arr);
        }
		
    	if($gid == 0){
    		$data['isshow'] = 1;   		
    		$r = D('info_game')->add($data);
    	}
		else{
    		$r = D('info_game')->where('gameid', $gid)->save($data);
    	}
    	
    	$this->ajaxReturn(['status' => 1,'msg' => '操作成功','data' =>['url'=>U("Admin/Information/informationlist")]]);
    }
	
}
?>